package com.shu.mq.dxl;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ReturnListener;
import com.shu.mq.ConnectionUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/9/14 14:38
 */

// 生产者
public class Producer {


    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        // 1 声明交换机
        String exchangeName = "test_dlx_exchange";
        String routingKeyPatten = "dlx.#";
        String queueName = "test_dlx_queue";
        String msg = "Hello RabbitMQ DLX Message";
        channel.exchangeDeclare(exchangeName, "topic", true, false, null);

        // 2 声明队列
        // 且队列 绑定 死信交换机以及死信交换机的路由
        Map<String, Object> agruments = new HashMap<String, Object>();
        agruments.put("x-dead-letter-exchange", "dlx.exchange");
        agruments.put("x-dead-letter-routing-key", "mail_queue_fail");//设置死信routingKey
        //这个agruments属性，要设置到声明队列上
        channel.queueDeclare(queueName, true, false, false, agruments);
        channel.queueBind(queueName, exchangeName, routingKeyPatten);

        // 3 要进行死信队列的声明
        channel.exchangeDeclare("dlx.exchange", "topic", true, false, null);
        channel.queueDeclare("dlx.queue", true, false, false, null);
        channel.queueBind("dlx.queue", "dlx.exchange", "#");

        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .deliveryMode(2)
                .contentEncoding("UTF-8")
                .expiration("10000")
                .build();

        // 4 发送消息
        channel.basicPublish(exchangeName, "dlx.save", true, properties, msg.getBytes());
    }
}